import express, { Request, Response } from 'express';
import qiniu from 'qiniu';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const port = process.env.PORT || 9876;


app.get('/', (req: Request, res: Response) => {
    res.send('qiniu server');
});
app.get('/getUpToken', (req: Request, res: Response) => {
    try {
        var mac = new qiniu.auth.digest.Mac(process.env.ACCESS_KEY, process.env.SECRET_KEY);
        var options = {
            scope: process.env.BUCKET_NAME,
        };
        var putPolicy = new qiniu.rs.PutPolicy(options);
        var uploadToken = putPolicy.uploadToken(mac);
        res.send({ code: 200, uptoken: uploadToken, msg: '获取成功' });
    } catch (error) {
        res.send({ code: 500, uptoken: error, msg: '获取失败' });
    }

});
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});
